package com.xjh.mapper;

import static com.xjh.mapper.StudentDynamicDynamicSqlSupport.stuName;
import static com.xjh.mapper.xjhTableDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import com.xjh.po.xjhTable;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;



@Mapper
public interface xjhTableMapper {
    BasicColumn[] selectList = BasicColumn.columnList(id, stuId, money, isEnable, createBy, createTime, updateBy, updateTime);

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    long count(SelectStatementProvider selectStatement);

    @DeleteProvider(type = SqlProviderAdapter.class, method = "delete")
    int delete(DeleteStatementProvider deleteStatement);

    @InsertProvider(type = SqlProviderAdapter.class, method = "insert")
    int insert(InsertStatementProvider<xjhTable> insertStatement);

    @InsertProvider(type = SqlProviderAdapter.class, method = "insertMultiple")
    int insertMultiple(MultiRowInsertStatementProvider<xjhTable> multipleInsertStatement);

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @ResultMap("xjhTableResult")
    Optional<xjhTable> selectOne(SelectStatementProvider selectStatement);

    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(id = "xjhTableResult", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "stu_id", property = "stuId", jdbcType = JdbcType.BIGINT),
            @Result(column = "money", property = "money", jdbcType = JdbcType.DECIMAL),
            @Result(column = "is_enable", property = "isEnable", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<xjhTable> selectMany(SelectStatementProvider selectStatement);

    @UpdateProvider(type = SqlProviderAdapter.class, method = "update")
    int update(UpdateStatementProvider updateStatement);

    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, xjhTable, completer);
    }

    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, xjhTable, completer);
    }

    default int deleteByPrimaryKey(Long id_) {
        return delete(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    default int insert(xjhTable record) {
        return MyBatis3Utils.insert(this::insert, record, xjhTable, c ->
                c.map(id).toProperty("id")
                        .map(stuId).toProperty("stuId")
                        .map(money).toProperty("money")
                        .map(isEnable).toProperty("isEnable")
                        .map(createBy).toProperty("createBy")
                        .map(createTime).toProperty("createTime")
                        .map(updateBy).toProperty("updateBy")
                        .map(updateTime).toProperty("updateTime")
        );
    }

    default int insertMultiple(Collection<xjhTable> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, xjhTable, c ->
                c.map(id).toProperty("id")
                        .map(stuId).toProperty("stuId")
                        .map(money).toProperty("money")
                        .map(isEnable).toProperty("isEnable")
                        .map(createBy).toProperty("createBy")
                        .map(createTime).toProperty("createTime")
                        .map(updateBy).toProperty("updateBy")
                        .map(updateTime).toProperty("updateTime")
        );
    }

    default int insertSelective(xjhTable record) {
        return MyBatis3Utils.insert(this::insert, record, xjhTable, c ->
                c.map(id).toPropertyWhenPresent("id", record::getId)
                        .map(stuId).toPropertyWhenPresent("stuId", record::getStuId)
                        .map(money).toPropertyWhenPresent("money", record::getMoney)
                        .map(isEnable).toPropertyWhenPresent("isEnable", record::getIsEnable)
                        .map(createBy).toPropertyWhenPresent("createBy", record::getCreateBy)
                        .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
                        .map(updateBy).toPropertyWhenPresent("updateBy", record::getUpdateBy)
                        .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
        );
    }

    default Optional<xjhTable> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, xjhTable, completer);
    }

    default List<xjhTable> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, xjhTable, completer);
    }

    default List<xjhTable> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, xjhTable, completer);
    }

    default Optional<xjhTable> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, xjhTable, completer);
    }

    static UpdateDSL<UpdateModel> updateAllColumns(xjhTable record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(record::getId)
                .set(stuId).equalTo(record::getStuId)
                .set(money).equalTo(record::getMoney)
                .set(isEnable).equalTo(record::getIsEnable)
                .set(createBy).equalTo(record::getCreateBy)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateBy).equalTo(record::getUpdateBy)
                .set(updateTime).equalTo(record::getUpdateTime);
    }

    static UpdateDSL<UpdateModel> updateSelectiveColumns(xjhTable record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(record::getId)
                .set(stuId).equalToWhenPresent(record::getStuId)
                .set(money).equalToWhenPresent(record::getMoney)
                .set(isEnable).equalToWhenPresent(record::getIsEnable)
                .set(createBy).equalToWhenPresent(record::getCreateBy)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateBy).equalToWhenPresent(record::getUpdateBy)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime);
    }

    default int updateByPrimaryKey(xjhTable record) {
        return update(c ->
                c.set(stuId).equalTo(record::getStuId)
                        .set(money).equalTo(record::getMoney)
                        .set(isEnable).equalTo(record::getIsEnable)
                        .set(createBy).equalTo(record::getCreateBy)
                        .set(createTime).equalTo(record::getCreateTime)
                        .set(updateBy).equalTo(record::getUpdateBy)
                        .set(updateTime).equalTo(record::getUpdateTime)
                        .where(id, isEqualTo(record::getId))
        );
    }

    default int updateByPrimaryKeySelective(xjhTable record) {
        return update(c ->
                c.set(stuId).equalToWhenPresent(record::getStuId)
                        .set(money).equalToWhenPresent(record::getMoney)
                        .set(isEnable).equalToWhenPresent(record::getIsEnable)
                        .set(createBy).equalToWhenPresent(record::getCreateBy)
                        .set(createTime).equalToWhenPresent(record::getCreateTime)
                        .set(updateBy).equalToWhenPresent(record::getUpdateBy)
                        .set(updateTime).equalToWhenPresent(record::getUpdateTime)
                        .where(id, isEqualTo(record::getId))
        );
    }

//    使用注解方式实现1对1

    @Select("select * from xjh_table")
    @Results(id = "xxx", value = {
            // 外层是主表字段
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "stu_id", property = "stuId", jdbcType = JdbcType.BIGINT),
            @Result(column = "money", property = "money", jdbcType = JdbcType.DECIMAL),
            @Result(column = "is_enable", property = "isEnable", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            // 新加一个result 作为从表实体映射
            // 参数1：主表中从表的属性名 参数2：主表中和从表关联的外键字段名 参数3：从表中查询从表信息的具体方法
            @Result(property = "studentDynamic", column = "stu_id", one = @One(select = "com.xjh.mapper.StudentDynamicMapper.findById", fetchType = FetchType.EAGER))
    })
    List<xjhTable> findAll();
}